import datetime
from django.views.generic.list_detail import object_detail, object_list
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.http import Http404
from django.utils.translation import ugettext as _
from models import Question
from forms import SubmitFAQForm
from amscms import settings
from amscms.core.utils.captcha import validate_captcha
from amscms.core.utils import navigation

def question_detail(request, slug, template_name='faq/question_detail.html', extra_context={}):
    """
    Displays an individual question.
    
    """

    return object_detail(
        request,
        template_name=template_name,
        extra_context=extra_context,
        slug=slug,
        slug_field='slug',
        queryset=Question.objects.active(user=request.user),
    )    
        
def question_list(request, template_name='faq/question_list.html',
					extra_context={}, group=False):
    '''
    Displays a list of all the questions.
    '''
    
    # NOTE:
    # The code shown here is NOT REALLY NEEDED, but it is a good example
    # of extending an app using extra_content and such.
    # Specifically note how we set the dict value and then allow the user
    # to pass along their own additional extra_context using 'update'.

    query_set = Question.objects.active(group=group, user=request.user)
    
    if len(query_set) == 0:
        return HttpResponseRedirect('/faq/submit_faq/')

    last_update = query_set.values('updated_on').order_by('-updated_on',)[0]    
    extra = { 'updated_on': last_update['updated_on'] }
 
    extra.update(extra_context)
    
    return object_list(
        request,
        template_name=template_name,
        extra_context=extra,
        queryset=query_set,
    )

def faq_list(request, template_name='faq/faq_list.html', extra_context={}):
    '''
    Display a typical FAQ view without group headers.
    Shows how to "extend" or "override" the default view supplied above.
    We also make sure this view is also overridable.
    '''
    extra = { 'page_title': _('FAQs') }
    extra.update(extra_context)
    navigation.breadcrumb(request, 'faq_list')
    return question_list(request, template_name=template_name, extra_context=extra)

def faq_list_by_group(request,
                       template_name='faq/faq_list_by_group.html',
                       extra_context={}):
    extra = { 'page_title': _('Grouped FAQs') }
    extra.update(extra_context)
    navigation.breadcrumb(request, 'faq_list')
    return question_list(request, group=True,
                          template_name=template_name, extra_context=extra)

def faq_list_by_topic(request,
                       template_name='faq/faq_list_by_group.html',
                       extra_context={}):
    extra = { 'page_title': _('Grouped FAQs') }
    extra.update(extra_context)
    navigation.breadcrumb(request, 'faq_list')
    topics, questions = Question.objects.list_by_topics()
    extra['topics'] = topics
    extra['questions'] = questions
    return render_to_response(template_name, extra, context_instance=RequestContext(request))
    #return question_list(request, group=True,
    #                      template_name=template_name, extra_context=extra)

def submit_faq(request, form_class=SubmitFAQForm,
             template_name="faq/submit_question.html",
             success_url="/faq/", extra_context={}):
    navigation.breadcrumb(request, 'faq_submit')
    if request.method == 'POST':
        form = form_class(data=request.POST)
        captcha_error = validate_captcha(request)
        if captcha_error is not None:
            context = {}
            context["error"] = _("Please enter the words in the following picture correctly")
            context["RECAPTCHA_PUBLIC_KEY"] = settings.RECAPTCHA_PUBLIC_KEY
            context["form"] = form
            return render_to_response(template_name, context, context_instance=RequestContext(request))
        if form.is_valid():
            question = form.save()
            if request.user.is_authenticated():
                question.created_by = request.user
            question.save()
            slug = question.slug
            question.slug = slug.replace("anon", "q%d" % question.id)
            question.save()
            context = {}
            context["message"] = _("Your question was submitted and will be reviewed by the site administrator for possible inclusion in the FAQ.")
            return faq_list_by_group(request, extra_context=context)
        else:
            context = {'form' : form}
            context.update(extra_context)
            return render_to_response(template_name, context, context_instance=RequestContext(request))
    else:
        form = form_class()
    context = { 'form': form }
    context["RECAPTCHA_PUBLIC_KEY"] = settings.RECAPTCHA_PUBLIC_KEY
    context.update(extra_context)
    return render_to_response(template_name, context,
                              context_instance=RequestContext(request))
